package LeetCode;

public class LC_114_FlattenBinaryTreetoLinkedList {

    public static void main(String[] args) {

    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Solution {

        public void flatten(TreeNode root) {
            while (root != null) {
                if (root.left != null) {
                    TreeNode pre = root.left;
                    while (pre.right != null)
                        pre = pre.right;
                    pre.right = root.right;
                    root.right = root.left;
                    root.left = null;
                }
                root = root.right;
            }
        }

    }

}